package edu.gxnu.cetcdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gxnu.cetcdemo.mapper.UserMapper;
import edu.gxnu.cetcdemo.pojo.User;
import edu.gxnu.cetcdemo.service.UserService;
import edu.gxnu.cetcdemo.utils.JwtHelper;
import edu.gxnu.cetcdemo.utils.MD5Util;
import edu.gxnu.cetcdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* author Uplifted
* description  针对表【user】的数据库操作Service实现
* createDate  2024-04-02 15:09:11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result register(User registerUser) {
        // 根据注册用户名查询数据库
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        User user = userMapper.selectOne(lambdaQueryWrapper.eq(User::getUserName, registerUser.getUserName()));

        // 注册失败
        if(user != null){
            return Result.build(501, "用户名已使用");
        }else{
            // 注册
            registerUser.setUserPassword(MD5Util.encrypt(registerUser.getUserPassword()));
            registerUser.setUserAvatarUrl("https://s2.loli.net/2024/04/15/xUz2TjIGfJmiaoq.jpg");
            if(userMapper.insert(registerUser) == 0) {
                return Result.build(502, "用户注册失败");
            }
            return Result.build(200, "用户注册成功");
        }
    }

    @Override
    public Result login(User user){
        // 根据用户名查询数据库
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        User loginUser = userMapper.selectOne(lambdaQueryWrapper.eq(User::getUserName, user.getUserName()));

        // 若用户名不存在
        if (loginUser == null) {
            return Result.build(501, "用户名不存在");
        }else{
            // 比较密码
            if (MD5Util.encrypt(user.getUserPassword()).equals(loginUser.getUserPassword())) {
                // 登录成功
                // 生成token
                String token = JwtHelper.getToken(loginUser.getUserId().toString());
                // 返回用户数据和token
                Map data = new HashMap();
                data.put("token", token);
                data.put("user", loginUser);

                return Result.build(200, "登录成功", data);
            }else{
                // 登录失败
                return Result.build(502, "密码错误");
            }
        }
    }

    @Override
    public Result queryProfile(long userId) {
        // 查询用户数据记录
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        User user = userMapper.selectOne(lambdaQueryWrapper.eq(User::getUserId, userId));

        if(user == null){
            return Result.build(501, "查询失败");
        }

        Map data = new HashMap<>();
        data.put("user", user);
        return Result.build(200, "查询成功", data);
    }

    @Override
    public Result updateProfile(User user) {
        if(userMapper.update(user, new LambdaQueryWrapper<User>().eq(User::getUserId, user.getUserId())) == 0){
            return Result.build(502, "个人信息修改失败");
        }
        return Result.build(200, "个人信息修改成功");
    }
}




